﻿<%@ WebHandler Language="C#" Class="OfferPriceHandler" %>

using System;
using System.Web;
using System.Collections;
using System.Data;
using StarTech.DBUtility;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Web.DynamicData;


public class OfferPriceHandler : IHttpHandler
{
    AdoHelper ado = AdoHelper.CreateHelper(StarTech.Util.AppConfig.DBInstance);
    string memberId = "";
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        if (context.Request.Cookies["memberInfo"] != null && Common.CheckUser(context.Request.Cookies["memberInfo"].Value))
        {
            memberId = context.Request.Cookies["memberInfo"].Value.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries)[0];

            string flag = Common.NullToEmpty(context.Request["flag"]);
            string goodsId = Common.NullToEmpty(context.Request["goodsId"]);
            string addressDetail = Common.NullToEmpty(context.Request["addressDetail"]);

            switch (flag.ToLower())
            {
                case "list":
                    context.Response.Write(List());
                    break;
                case "unread":
                    context.Response.Write(MsgListUnRead());
                    break;
                case "listbygoods":
                    context.Response.Write(ListByGoods(goodsId));
                    break;
                case "sign":
                    context.Response.Write(SignRecord(goodsId,addressDetail));
                    break;
                case "msglist":
                    context.Response.Write(MsgList());
                    break;
                case "msgdetail":
                    string msgid = Common.NullToEmpty(context.Request["msgid"]);
                    context.Response.Write(MsgDetail(msgid));
                    break;
            }
        }
        else
        {
            context.Response.Write("please login");
        }
    }

    private string List()
    {
        string strSql = @"select a.*,b.GoodsName,c.TrueName from T_Member_JobRecord a
left join T_Goods_Info b on a.GoodsId=b.GoodsId 
left join T_Member_Info c on a.MemberId=c.MemberId 
where a.GoodsId in (select GoodsId from T_Goods_Info where ProviderInfo=@MemberId and (JobStatus='进行中' or JobStatus='竞价中'))";

        SqlParameter[] p = { 
                               new SqlParameter("@MemberId", memberId)
                           };
        return JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);
    }

    private string ListByGoods(string goodsId)
    {
        string strSql = @"select a.*,b.GoodsName,c.TrueName from T_Member_JobRecord a
left join T_Goods_Info b on a.GoodsId=b.GoodsId 
left join T_Member_Info c on a.MemberId=c.MemberId 
where a.GoodsId = @goodsId order by RecordTime";

        SqlParameter[] p = { 
                               new SqlParameter("@MemberId", memberId),
                               new SqlParameter("@goodsId", goodsId)
                           };
        return JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);
    }

    private string MsgList()
    {
        string strSql = @"select * from T_Log_Message where MemberId=@MemberId order by isread,createtime desc";

        SqlParameter[] p = { 
                               new SqlParameter("@MemberId", memberId)
                           };
        return JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);
    }

    private string MsgListUnRead()
    {
        string strSql = @"select * from T_Log_Message where MemberId=@MemberId and (isRead is null or isRead<>'1') order by createtime desc";

        SqlParameter[] p = { 
                               new SqlParameter("@MemberId", memberId)
                           };
        return ado.ExecuteSqlDataset(strSql, p).Tables[0].Rows.Count.ToString();
    }

    private string MsgDetail(string msgid)
    {
        string strSql = @"select * from T_Log_Message where MemberId=@MemberId and sysnumber=@msgid";

        SqlParameter[] p = { 
                               new SqlParameter("@MemberId", memberId),
                               new SqlParameter("@msgid", msgid)
                           };
        string result= JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);

        strSql = @"update T_Log_Message set isread='1' where MemberId=@MemberId and sysnumber=@msgid";
        SqlParameter[] p2 = { 
            new SqlParameter("@MemberId", memberId),
            new SqlParameter("@msgid", msgid)
        };
        ado.ExecuteSqlNonQuery(strSql, p2);
        
        return result;
    }

    private string SignRecord(string goodsId, string addressDetail)
    {
        string strSql = @"select RecordTime from T_Member_JobRecord where GoodsId=@GoodsId and MemberId=@MemberId";
        SqlParameter[] p = { 
                               new SqlParameter("@MemberId", memberId),
                               new SqlParameter("@GoodsId", goodsId)
                           };
        DataTable dt = ado.ExecuteSqlDataset(strSql, p).Tables[0];
        if (dt.Rows.Count > 0)
        {
            if (Convert.ToDateTime(dt.Rows[0]["RecordTime"]).ToShortDateString().Equals(DateTime.Now.ToShortDateString()))
            {
                return "今天已经签到过了，无需重复签到";
            }
        }

        string recordId = Guid.NewGuid().ToString();
        string JobAddressCode = "", JobAddress = "", GoodsName = "", ProviderInfo = "";
        strSql = @"select GoodsName,JobAddressCode,JobAddress,TrueName,ProviderInfo,a.JobStartTime from T_Goods_Info a
                left join T_Goods_MemberPrice c on a.goodsId=c.goodsId
                left join T_Member_Info b on c.MemberId=b.MemberId where a.goodsId=@goodsId";
        SqlParameter[] p1 = { new SqlParameter("@goodsId", goodsId) };
        DataTable dt2 = ado.ExecuteSqlDataset(strSql, p1).Tables[0];
        if (dt2.Rows.Count > 0)
        {
            JobAddressCode = Common.NullToEmpty(dt2.Rows[0]["JobAddressCode"]);
            JobAddress = string.IsNullOrEmpty(addressDetail) ? Common.NullToEmpty(dt2.Rows[0]["JobAddress"]) : addressDetail;
            GoodsName = Common.NullToEmpty(dt2.Rows[0]["GoodsName"]);
            ProviderInfo = Common.NullToEmpty(dt2.Rows[0]["ProviderInfo"]);

            if (Convert.ToDateTime(dt2.Rows[0]["JobStartTime"])>DateTime.Now)
            {
                return "尚未到任务日期，不可签到";
            }
        }

        strSql = @"INSERT INTO T_Member_JobRecord(RecordId,MemberId,RecordTime,GoodsId,JobAddressCode,JobAddress,RecordInfo)
VALUES(@RecordId,@MemberId,getdate(),@GoodsId,@JobAddressCode,@JobAddress,@RecordInfo)";

        SqlParameter[] p2 = { 
                               new SqlParameter("@RecordId", recordId), 
                               new SqlParameter("@MemberId", memberId),
                               new SqlParameter("@GoodsId", goodsId),
                               new SqlParameter("@JobAddressCode", JobAddressCode),
                               new SqlParameter("@JobAddress", JobAddress),
                               new SqlParameter("@RecordInfo", "签到")
                           };
        int result = ado.ExecuteSqlNonQuery(strSql, p2);
        if (result == 1)
        {
            string TrueName = ado.ExecuteSqlScalar("select top 1 TrueName from T_Member_Info where MemberId='" + memberId + "'").ToString();

            string jobType = Common.GetJobTypeByGoodsId(goodsId);
            Common.AddLogMsg(ProviderInfo, "雇员签到", "【" + GoodsName + "】任务中的雇员 " + TrueName + " 今日已签到<br />地点：" + JobAddress, "Task" + (jobType == "DG" ? "People" : "") + "Info.html?gid=" + goodsId, 0);
            return "签到成功";
        }
        else
            return "签到失败";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}